﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Sofire.DataComm.Remoting
{
    /// <summary>
    /// 表示一个远程操作的返回值内容。
    /// </summary>
    public class RemotingResult
    {
        private ErrorCode _ErrorCode;
        /// <summary>
        /// 设置或获取一个值，表示远程操作的错误代码。
        /// </summary>
        public ErrorCode ErrorCode
        {
            get
            {
                return this._ErrorCode;
            }
            set
            {
                this._Exception = null;
                this._ErrorCode = value;
            }
        }

        private string _Exception;
        /// <summary>
        /// 设置或获取远程调用的错误信息。
        /// </summary>
        public Exception Exception
        {
            get
            {
                if(string.IsNullOrEmpty(this._Exception))
                {
                    if(this._ErrorCode == ErrorCode.Success) return null;
                    return new Exception(this._ErrorCode.ToString());
                }
                return new Exception(this._Exception);
            }
            set
            {
                this._ErrorCode = ErrorCode.Success;
                if(value == null) this._Exception = null;
                else
                {
#if DEBUG
                    this._Exception = value.ToString();
#else
                    this._Exception = value.Message;
#endif
                }
            }
        }

        private bool _IsTerminated;
        /// <summary>
        /// 设置或获取一个值，表示是否终止连接。
        /// </summary>
        public bool IsTerminated
        {
            get
            {
                return this._IsTerminated;
            }
            set
            {
                this._IsTerminated = value;
            }
        }

        private long _Token;
        /// <summary>
        /// 获取一个值，表示调用的标识。
        /// </summary>
        public long Token
        {
            get
            {
                return this._Token;
            }
        }

        private object _Value;
        /// <summary>
        /// 设置或获取一个值，表示远程操作的返回值。
        /// </summary>
        public object Value
        {
            get
            {
                return this._Value;
            }
            set
            {
                this._Value = value;
            }
        }

        private RemotingResult() { }

        internal RemotingResult(long token)
        {
            this._Token = token;
        }

        internal RemotingResult(long token, ErrorCode errorCode, bool isTerminate)
            : this(token)
        {
            this._ErrorCode = errorCode;
            this._IsTerminated = isTerminate;
        }
        internal RemotingResult(long token, ErrorCode errorCode)
            : this(token, errorCode, false) { }

        internal RemotingResult(long token, object value)
            : this(token)
        {
            this._Value = value;
        }

    }
}
